<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PartyUser;
use App\Models\SystemRegion;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class PartyUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PartyUser(), function (Grid $grid) {
            $grid->model()->orderBy('status', 'desc')->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('username');
            $grid->column('avatar')->image('', 60, 60);
            $grid->column('identity');
            $grid->column('address');
            $grid->column('status','是否展示')->switch()->sortable();
            $grid->column('region_id', '所属地区')->display(function ($region_id) {
                if ($region_id) {
                    $region = \App\Models\SystemRegion::find($region_id);
                    return $region ? $region->region_name : '';
                } else {
                    return '';
                }
            });
            $grid->column('updated_at')->sortable();

            $grid->disableViewButton(); // 禁用详情按钮
            $grid->quickSearch(['username', 'identity', 'address'])->placeholder('搜索...');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PartyUser(), function (Show $show) {
            $show->field('id');
            $show->field('username');
            $show->field('avatar');
            $show->field('identity');
            $show->field('address');
            $show->field('status');
            $show->field('region_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PartyUser(), function (Form $form) {
            $form->display('id');
            $form->text('username');
            $form->image('avatar')->move('party/'.date('Y-m-d'))->autoUpload();
            $form->text('identity');
            $region_id = Admin::user()->region_id;
            if ($region_id) {
                // 非总管理员
                $region = SystemRegion::find($region_id);
                $form->select('region_id', '所属地区')->options([$region_id => $region->region_name])->default($region_id, true);
            } else {
                $region = \App\Admin\Repositories\SystemRegion::getList();
                $form->select('region_id', '所属地区')->options($region)->default(0, true);
            }
            $form->text('address');
            $form->radio('status')->options([1 => '显示', 0 => '隐藏'])->default(1);
        
            $form->disableViewCheck(true);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
